package com.pro.controller;

import com.pro.pojo.*;
import com.pro.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * @ClassName: ProductController
 * @Author: Kai
 * @DATE: 2022/1/7
 * @TIME: 下午 01:57
 */

@RestController
@RequestMapping(value = "/Product")
public class ProductController {

    @Autowired
    private ProductService productService;

    //查询所有商品分类
    @RequestMapping(value = "/getAllProductCategroy")
    public JsonData getAllProductCategroy(){
        List<ProductCategroy> allProductCategroy = productService.getAllProductCategroy();
        return allProductCategroy.size() > 0 ? JsonData.buildSuccess(allProductCategroy) : JsonData.buildError("没有查到");
    }

    //根据id删除商品分类
    @RequestMapping(value = "/deleteProductCategroy")
    public JsonData deleteProductCategroy(Integer categoryId){
        Boolean aBoolean = null;
        try {
            aBoolean = productService.deleteProductCategroy(categoryId);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return aBoolean ? JsonData.buildSuccess(aBoolean) : JsonData.buildError("删除失败");
    }

    //添加一条商品分类
    @RequestMapping(value = "/addProductCategroy")
    public JsonData addProductCategroy(ProductCategroy productCategroy){
        Boolean aBoolean = productService.addProductCategroy(productCategroy);
        return aBoolean ? JsonData.buildSuccess(aBoolean) : JsonData.buildError("删除失败");
    }

    //根据id修改一条商品分类
    @RequestMapping(value = "/updateProductCategroy")
    public JsonData updateProductCategroy(ProductCategroy productCategroy){
        Boolean aBoolean = null;
        try {
            aBoolean = productService.updateProductCategroy(productCategroy);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return aBoolean ? JsonData.buildSuccess(aBoolean) : JsonData.buildError("删除失败");
    }

    //查询商品
    @RequestMapping(value = "/getProduct")
    public JsonData getProduct(Product product){
        List<Product> allProduct = productService.getProduct(product);
        return allProduct.size() > 0 ? JsonData.buildSuccess(allProduct) : JsonData.buildError("没有查到");
    }

    //根据id查询商品的详情信息
    @RequestMapping(value = "/getByIdProduct")
    public JsonData getByIdProduct(@RequestParam Map<String, Integer> map) {
        Product byIdProduct = productService.getByIdProduct(map);
        return byIdProduct != null ? JsonData.buildSuccess(byIdProduct) : JsonData.buildError("没有查到");
    }

    //根据id修改商品
    @RequestMapping(value = "/updateProduct")
    public JsonData updateProduct(Product product, JsCourseJournal jsCourseJournal) {
        Boolean aBoolean = null;
        try {
            aBoolean = productService.updateProduct(product, jsCourseJournal);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return aBoolean ? JsonData.buildSuccess(aBoolean) : JsonData.buildError("修改失败");
    }

    //根据商品id查询商品状态
    @RequestMapping(value = "/getCourseRecord")
    public JsonData getCourseRecord(Integer id, Integer type) {
        List<CourseRecord> courseRecord = productService.getCourseRecord(id, type);
        return courseRecord.size() > 0 ? JsonData.buildSuccess(courseRecord) :JsonData.buildError("没有查到");
    }

    //根据商品id删除商品
    @RequestMapping(value = "/deleteProduct")
    public JsonData deleteProduct(Integer id) throws Exception {
        Boolean deleteProductBoolean = productService.deleteProduct(id);
        return deleteProductBoolean ? JsonData.buildSuccess(deleteProductBoolean) : JsonData.buildError("删除失败啊");
    }

    //添加商品
    @RequestMapping(value = "/addProduct")
    public JsonData addProduct(Product product, CourseRecord courseRecord) {
        Boolean productBoolean = null;
        try {
            productBoolean = productService.addProduct(product, courseRecord);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return productBoolean ? JsonData.buildSuccess(productBoolean) : JsonData.buildError("添加失败");
    }

    //添加商品操作记录
    @RequestMapping(value = "/addProductOperationRecord")
    public JsonData addProductOperationRecord(OperationLog operationLog) {
        Boolean addProductOperationRecordBoolean = null;
        try {
            addProductOperationRecordBoolean = productService.addProductOperationRecord(operationLog);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return addProductOperationRecordBoolean ? JsonData.buildSuccess(addProductOperationRecordBoolean) : JsonData.buildError("添加失败");
    }


    /* 功能描述: <br>
     * 〈查询商品的操作记录〉
     *
     * @param: operationLog
     * @return: com.pro.pojo.JsonData
     * @Author: Kai
     * @Date: 2022/1/12 下午 04:22
     */
    @RequestMapping(value = "/getProductOperationLogs")
    public JsonData getProductOperationLogs(OperationLog operationLog) {
        List<OperationLog> productOperationLogs = productService.getProductOperationLogs(operationLog);
        return productOperationLogs.size() > 0 ? JsonData.buildSuccess(productOperationLogs) : JsonData.buildError("添加失败");
    }

    /* 功能描述: <br>
     * 〈根据id删除商品操作记录〉
     *
     * @param id
     * @return: com.pro.pojo.JsonData
     * @Author: Kai
     * @Date: 2022/1/12 下午 04:41
     */
    @RequestMapping(value = "/deleteProductOperationLog")
    public JsonData deleteProductOperationLog(Integer id){
        Boolean deleteProductOperationLogBoolean = null;
        try {
            deleteProductOperationLogBoolean = productService.deleteProductOperationLog(id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return deleteProductOperationLogBoolean ? JsonData.buildSuccess(deleteProductOperationLogBoolean) : JsonData.buildError("删除失败");
    }

    /* 功能描述: <br>
    * 〈根据名字查询id〉
    *
    * @param name
    * @return: com.pro.pojo.JsonData
    * @Author: Kai
    * @Date: 2022/1/12 下午 06:30
    */
    @RequestMapping(value = "/getByNameId")
    public JsonData getByNameId(String name){
        List<Integer> userId = productService.getByNameId(name);
        return userId.size() > 0 ? JsonData.buildSuccess(userId) : JsonData.buildError("删除失败");
    }

    /* 功能描述: <br>
    * 〈根据用户id获取商品列表〉
    *
    * @param product
    * @return: com.pro.pojo.JsonData
    * @Author: Kai
    * @Date: 2022/1/13 下午 03:09
    */
    @RequestMapping(value = "/getByUserIdProductLists")
    public JsonData getByUserIdProductLists(Product product){
        List<Product> byUserIdProductLists = productService.getByUserIdProductLists(product);
        return byUserIdProductLists.size() > 0 ? JsonData.buildSuccess(byUserIdProductLists) : JsonData.buildError("没有查找到");
    }
}
